---@class Battle.EffectMgr
local EffectShowMgr = Battle.CreateClass("EffectMgr")
Battle.EffectShowMgr = EffectShowMgr
local this = EffectShowMgr
function EffectShowMgr.OnCreate()
    ---@type Battle.EffectBase[]
    this.effectList = {}
    ---@type Battle.EffectBase[]
    this.tempList = {}
    this.groupChain = {{},{}}
    this.Register()
end
function EffectShowMgr.OnDestroy()
    this.UnRegister()
    if this.effectList then
        for k,v in pairs(this.effectList) do
            v:OnDestroy()
        end
    end
    this.tempList = nil
end

function EffectShowMgr.Register()
    Battle.ClientBattle:RegisterUpdateCallback(this.Update)
    Battle.GlobalEventSystem.Register(Battle.Event.BulletCreate,this.Event_BulletCreate)
    Battle.GlobalEventSystem.Register(Battle.Event.BuffCreate,this.Event_BuffCreate)
    Battle.GlobalEventSystem.Register(Battle.Event.BuffActive,this.Event_BuffActive)
    Battle.GlobalEventSystem.Register(Battle.Event.BuffDestroy,this.Event_BuffDestroy)

end
function EffectShowMgr.UnRegister()
    Battle.GlobalEventSystem.UnRegister(Battle.Event.BulletCreate,this.Event_BulletCreate)
    Battle.GlobalEventSystem.UnRegister(Battle.Event.BuffCreate,this.Event_BuffCreate)
    Battle.GlobalEventSystem.UnRegister(Battle.Event.BuffActive,this.Event_BuffActive)
    Battle.GlobalEventSystem.UnRegister(Battle.Event.BuffDestroy,this.Event_BuffDestroy)

end
---@return Battle.EffectBase
function EffectShowMgr.PlayEffect(resName, pos, forward, during)
    local effect = Battle.EffectFactory.CreateEffect(Battle.EffectType.effect,resName,pos,forward,during)
    if effect == nil then return nil end
    this.AddEffect(effect)
    effect:Execute()
    return effect
end
---@param effect Battle.EffectBase
function EffectShowMgr.DestroyEffect(effect)
    if not effect then return end
    this.effectList[effect.id] = nil
    effect:OnDestroy()
    Battle.PoolMgr.ReturnEffectToPool(effect)
end
---@param effect Battle.EffectBase
function EffectShowMgr.AddEffect(effect)
    if effect.id == 0 then
        logError("[AddEffect] id error is 0")
    end
    this.effectList[effect.id] = effect
end
function EffectShowMgr.Update(dt)
    for k,v in pairs(this.effectList) do
        v:Update(dt)
        if v:IsDie() then
            table.insert(this.tempList,v)
        end
    end
    for k,v in pairs(this.tempList) do
        this.DestroyEffect(v)
    end
    table.clear(this.tempList)
end
---@return Battle.ChainEffectGroup
function EffectShowMgr.GetGroupChain(camp, sign)
    local list = this.groupChain[camp]
    return list[sign]
end
function EffectShowMgr.AddGroupChain(camp, sign, group)
    local list = this.groupChain[camp]
    list[sign] = group
end
---@param args BulletArgs
function EffectShowMgr.Event_BulletCreate(args)
    local bullet = args.bullet
    local showCfg = DataTable.GetById("t_skillshow",bullet.skill.skillId)
    local resName = showCfg.f_BulletSE
    if string.IsEmpty(resName) then return end
    local effectType = Battle.EffectType.bullet
    if showCfg.f_SpecialEffectsType == 2 then
        effectType = Battle.EffectType.rayEffect
    end
    local effect = Battle.EffectFactory.CreateEffect(effectType,resName,bullet.pos,bullet.forward)
    effect:BindBullet(bullet)
    this.AddEffect(effect)
    effect:Execute()
end
---@param args Battle.BufferArgs
function EffectShowMgr.Event_BuffCreate(args)
    local buff = args.buff
    local cfg = DataTable.GetById("t_buffshow",buff.buffId)
    if not cfg then return end
    local actor = Battle.ActorManager.GetActor(buff.targetId)
    if cfg.f_Stroke == 1 then
        actor.gameObjectSystem:ShowOutLine(true)
    end
    if cfg.PlayOpportunity1 == 1 and not string.IsEmpty(cfg.f_NormalBuff)  then
        this.CreateBuffShow(buff,cfg.f_BuffType,cfg.f_NormalBuff)
    end
    if cfg.PlayOpportunity2 == 1 and not string.IsEmpty(cfg.f_NormalBuff2)  then
        this.CreateBuffShow(buff,cfg.f_BuffType2,cfg.f_NormalBuff2)
    end
end
---@param args Battle.BufferArgs
function EffectShowMgr.Event_BuffActive(args)
    local buff = args.buff
    local cfg = DataTable.GetById("t_buffshow",buff.buffId)
    if not cfg then return end
    if cfg.PlayOpportunity1 == 2 and not string.IsEmpty(cfg.f_NormalBuff)  then
        this.CreateBuffShow(buff,cfg.f_BuffType,cfg.f_NormalBuff)
    end
    if cfg.PlayOpportunity2 == 2 and not string.IsEmpty(cfg.f_NormalBuff2)  then
        this.CreateBuffShow(buff,cfg.f_BuffType2,cfg.f_NormalBuff2)
    end
end
---@param buff Battle.Buffer
function EffectShowMgr.CreateBuffShow(buff,resType,resName)
    local actor = Battle.ActorManager.GetActor(buff.targetId)
    if resType == 2 then -- 连接
        local sign = buff:GetSign()
        if sign > 0 then
            local group = this.GetGroupChain(actor.camp,sign)
            if not group then
                group = Battle.ChainEffectGroup.New(resName,sign)
                this.AddGroupChain(actor.camp,sign,group)
            end
            group:AddActor(actor)
            group:Execute()
        end
    elseif resType == 3 then--从源到目标随机曲线
        local effect = Battle.EffectFactory.CreateEffect(Battle.EffectType.A2BRandom,resName,nil,nil,buff.during)
        effect:BindBuff(buff)
        this.AddEffect(effect)
        effect:Execute()
    else
        local effect = Battle.EffectFactory.CreateEffect(Battle.EffectType.BufferShow,resName,nil,nil,buff.during)
        effect:BindBuff(buff)
        this.AddEffect(effect)
        effect:Execute()
    end
end
---@param args Battle.BufferArgs
function EffectShowMgr.Event_BuffDestroy(args)
    local buff = args.buff
    local cfg = DataTable.GetById("t_buffshow",buff.buffId)
    if not cfg then return end
    if cfg.f_Stroke == 1 then
        args.sender.gameObjectSystem:ShowOutLine(false)
    end
end